package com.shankungfu.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.shankungfu.GlobleConstant;
import com.shankungfu.R;
import com.shankungfu.adapter.LetterAdapter;
import com.shankungfu.bean.LetterBean;
import com.shankungfu.bean.PersonalLetterListDataBean;
import com.shankungfu.fragment.MessageFragment;
import com.shankungfu.util.MyAsyncHttpResponseHandler;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.util.WebService;

public class PersonalLetterAcitvity extends Activity implements OnClickListener {

	private static final int data_success = 1;
	private static final int data_error = 2;

	private EditText contentET;
	private Button send_letter;
	private String user_id;
	private String personal_letter_id;
	private ListView letterListview;
	private LetterAdapter adapter;
	private RelativeLayout progressBar;
	private String user_img_url;
	private ArrayList<PersonalLetterListDataBean> letterList;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
			case data_success:

				LetterBean bean = (LetterBean) msg.obj;

				letterList = bean.getPersonalLetterListData();
				adapter.setData(letterList);
				adapter.notifyDataSetChanged();
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_letter_layout);

		initView();
		updateForData();
		getLetterData();
	}

	private void initView() {

		user_id = getIntent().getStringExtra(MessageFragment.USER_ID);
		personal_letter_id = getIntent().getStringExtra(
				MessageFragment.PERSONAL_LETTER_ID);
		user_img_url = getIntent().getStringExtra(MessageFragment.USER_IMG_URL);

		TextView title = (TextView) findViewById(R.id.detail_title);
		findViewById(R.id.head_refresh).setVisibility(View.GONE);
		findViewById(R.id.iv).setVisibility(View.GONE);
		letterListview = (ListView) findViewById(R.id.letter_listview);
		progressBar = (RelativeLayout) findViewById(R.id.progressBar);
		send_letter = (Button) findViewById(R.id.send_letter);

		contentET = (EditText) findViewById(R.id.content);

		letterList = new ArrayList<PersonalLetterListDataBean>();

		adapter = new LetterAdapter(this, letterList);
		letterListview.setAdapter(adapter);

		title.setText("私信");
	}

	private void getLetterData() {
		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "personal_letter");
		params.add("app_info", "shankungfu");
		params.add("dialouge_id", personal_letter_id);
		AsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				getSuccessData();
				if (!TextUtils.isEmpty(content)
						&& content.contains("\"result\":0")) {

					LetterBean bean = JSON.parseObject(content,
							LetterBean.class);
					Message msg = new Message();
					msg.what = data_success;
					msg.obj = bean;
					handler.sendMessage(msg);
				} else {
					handler.sendEmptyMessage(data_error);
				}
			}

			@Override
			public void onFailure(String content) {
				handler.sendEmptyMessage(data_error);
			}

		};
		httpclient.request(GlobleConstant.INBOX_DIALOGUE, params,
				responseHandler);
	}

	private void updateForData() {
		progressBar.setVisibility(View.VISIBLE);
	}

	private void getSuccessData() {
		progressBar.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_goback:
			finish();
			break;
		case R.id.send_letter: // 点击发送私信
			updateForData();
			sendLetter();
			break;
		}
	}

	private void sendLetter() {
		final String contentStr = contentET.getText().toString();

		if (!TextUtils.isEmpty(contentStr)) {
			contentET.setText("");

			WebService httpclient = new WebService();
			RequestParams params = new RequestParams();
			params.add("serviceId", "send_message");
			params.add("app_info", "shankungfu");
			params.add("sendto_uid", user_id);
			params.add("message", contentStr);
			MyAsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {
				@Override
				public void onSuccess(String content) {
					getSuccessData();
					if (!TextUtils.isEmpty(content)
							&& content.contains("\"result\":0")) {

						ToastUtil.showMessageText(PersonalLetterAcitvity.this,
								"发送成功");
						LetterBean bean = JSON.parseObject(content,
								LetterBean.class);
						Message msg = new Message();
						msg.what = data_success;
						msg.obj = bean;
						handler.sendMessage(msg);

						getLetterData();
					} else {
						ToastUtil.showMessageText(PersonalLetterAcitvity.this,
								"发送失败");
						handler.sendEmptyMessage(data_error);
					}
				}

				@Override
				public void onFailure(String content) {
					ToastUtil.showMessageText(PersonalLetterAcitvity.this,
							"发送失败");
					handler.sendEmptyMessage(data_error);
				}

			};
			responseHandler.setShowNotNetToast(false);
			httpclient.request(GlobleConstant.SEND_MESSAGE, params,
					responseHandler);
		} else {
			ToastUtil.showMessageText(PersonalLetterAcitvity.this, "发送内容不能为空");
		}
	}

}
